Ext.namespace("NewCustomer");

NewCustomer.Search3 = function() {
	var ps = new ProjectSourceDemo();
	
	this.custID = null;
	this.Birthday = null;
	this.winCustomer = null;
	this.winView = null;
	this.txtSuperviseTitle = new Ext.form.TextField({
		name : 'superviseTitle',
		width: 150
	});
	this.storeType  = new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: OMS.ISSUE_TYPE2
    });
	this.txtIssueType = new Ext.form.ComboBox({
		width: 150,
		name:'issueType',
		store: this.storeType,
        valueField: 'value',
        displayField: 'name',
        editable: false,
        selectOnFocus: true
	});
	this.dtStart = new Ext.form.DateField({
		width: 85,
		name : 'issueDateFm',
		format: 'Y-m-d'
	});
	this.dtEnd = new Ext.form.DateField({
		width: 85,
		name : 'issueDateTo',
		format: 'Y-m-d'
	});
	this.txtState = new Ext.form.ComboBox({
		width: 150,
		name:'superviseState',
        store: new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: OMS.SUPERVISE_STATE2
        }),
        valueField: 'value',
        displayField: 'name',
        editable: false,
        mode: 'local',
        triggerAction: 'all'
	});	
    this.btnSearch = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查找',
        tooltip: {text: '查找下载资料'},
        handler: this.queryIssue,
        scope: this
    });
	/**
	4-5 林翊增加
	**/	
	this.btnNew = new Ext.Button({
		text: '新建个人客户',
		iconCls: 'icon-newplan',
		scope: this,
		handler: this.onNew
	});
	this.btnEdit = new Ext.Button({
		text: '修改个人客户',
		iconCls: 'icon-submitplan',
		scope: this,
		handler: this.onEdit
	});
	this.btnGiveup = new Ext.Button({
		text: '下放个人客户',
		iconCls: 'icon-submitplan',
		scope: this,
		handler: this.onGiveup
	});	
	var CustomerId = new Ext.form.TextField({
		fieldLabel : '客户名称',
		width : 150
	});
		
	this.store = new Ext.data.JsonStore({
	    url : '../customer/searchCustomer.form?status=10&timesnap=' + Date.parse(new Date()),
	    root: 'result',
	    totalProperty: 'total',
        remoteSort: true,
        fields: ['id', 'CustomerName', 'Area',
				 'CustomerSize', 'CustomerType', 'CustomerAddress',
				 'FirstContact', 'Mobile', 'Leader', 'EmployCount',
				 'CompanyPhone', 'MSN', 'Email', 'Fax',
				 'ContactLike', 'Birthday', 'SecondContact',
				 'SecondMobile', 'PhoneMoney', 'MailMoney', 'NetMoney',
				 'SuccessTime', 'Stage', 'VsName',
				 'NsManager', 'VsTime', 'VsGood', 'VsBad',
				 'CreateTime','CreateBy','LastUpdateTime','LastUpdateBy'
        		],
        listeners : {
					scope : this,
					beforeload : function(thiz) {
					thiz.baseParams = {
						stage : 1}
						}
					}
	});
	//this.store.setDefaultSort('DocId', 'desc');
	this.renderType = function(value){
		return OMS.FILE_TYPE[value];
	};
	this.renderLevel = function(value){
		return OMS.FILE_LEVEL[value];
	};
	this.renderStatus = function(value){
		return OMS.SUPERVISE_STATE[value];
	};
	this.column = [
		 {header:'客户名称',width: 150,sortable: true,dataIndex:'CustomerName'},
         {header:'所属区域',width: 100, sortable: true,dataIndex:'Area',renderer:function(value){return OMS.AREA[value];}},
         {header:'项目类型',dataIndex:'CustomerType',renderer:function(value,row){return OMS.CUSTOMERTYPE[value];}},
         {header:'公司地址',dataIndex:'CustomerAddress'},
         {header:'联系人姓名',width: 170,dataIndex:'FirstContact'},
         {header:'联系人电话',dataIndex:'Mobile'}
	];	
			
	NewCustomer.Search3.superclass.constructor.call(this, {
		title: '个人数据库',
        region: 'center',
        cm: new Ext.grid.ColumnModel(this.column),
        sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
        tbar: [	  this.btnNew,this.btnEdit,this.btnGiveup,
        		  '->','资料类别:', this.txtIssueType, 
		          '上传时间:', this.dtStart, '~', this.dtEnd,this.btnSearch],
        bbar:new Ext.PagingToolbar({
			 pageSize: 15,
	         store: this.store,
	         displayInfo: true,
	         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
	         emptyMsg: "没有记录..."
		}),
        store:this.store,
        viewConfig: { forceFit: true },
        listeners:{
        	rowmousedown : function(grid,index,e){
        		var data = grid.store.getAt(index).data;
        		custID=data.id;
        	},
        	rowdblclick : this.onViewIssue,
			//render:this.queryIssue
			activate: this.queryIssue
		}
    });
};

		
Ext.extend(NewCustomer.Search3, Ext.grid.GridPanel, {
	queryIssue:function(btn,event){
		var param={
			start : 0,
			limit : 15,
			issueDateFm:this.dtStart.getValue(),
			issueDateTo:this.dtEnd.getValue(),
			issueType:this.txtIssueType.getValue(),
			superviseState:this.txtState.getValue()
		};
		this.store.load({
			params:param
		});
	},	
	onViewIssue:function(grid, rowIndex,e){		
		if (this.winView == null) {
			this.winView = new NewCustomer.View(this);
		}
		this.winView.show();
		this.winView.initWin(grid.store.getAt(rowIndex).data);		
	},//4-5 林翊增加
	onNew: function(btn, event) {
		if (this.winCustomer == null) {
			this.winCustomer = new NewCustomer.NewCustomer(this);
		}
		this.winCustomer.show();
		this.winCustomer.initWin();		
	},
	onGiveup: function(btn, event) {
		var records = this.getSelectionModel().getSelections();
		if(records.length==0){
			Utils.warn2('请选择一条记录！',180);
			return ;
		}
		alert(custID);
	    var url = '../customer/giveUpCustomer.form?timesnap=' + Date.parse(new Date())+'&custID='+custID;
	    scope=this;
	    	Utils.confirm2({
				msg:'是否下放公司客户?',
				width:250,
				fn:function(){
			        scope.submitIssue(url)
					this.queryIssue;
			       	Utils.info("操作成功","系统提示");	
					}		
		})
	},
	submitIssue:function(url){
		var records = this.getSelectionModel().getSelections();
		if(records.length==0){
			Utils.warn2('请选择一条记录！',180);
			return ;
		}
		var ids='';
		for(var i=0;i<records.length;i++){
			ids+=records[i].data['superviseId']+',';
		}
		scope=this;
		Ext.Ajax.request({
		   url: url,
		   params: {remind:this.remind,ids:ids},
		   success: function(){
			Utils.info2();
		   	scope.queryIssue();
		   }
		});
	}
});
